package com.atguigu.dao.impl;

import com.atguigu.bean.Book;
import com.atguigu.bean.Page;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.BookDao;

import java.util.List;

/**
 * @ Author:任留辉
 * @ Description:
 * @ Date:Created in 17:10 2021/3/16 0016
 */
public class BookDaoImpl extends BaseDao<Book> implements BookDao {
 /*   @Override
    public List<Book> getBookList() {
        String sql = "select id,title,author,price,sales,stock,img_path imgPath from bs_book";
        return getBeanList(Book.class, sql  );
    }*/

    @Override
    public void deleteBook(int bookId) {
        String sql = "delete from bs_book where id = ?";
        update(sql, bookId);
    }

    @Override
    public void addBook(Book book) {
        String sql = "insert into bs_book values(null,?,?,?,?,?,?)";
        update(sql,book.getTitle(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath());

    }

    @Override
    public Book getBookById(int bookId) {
        String sql = "select id,title,author,price,sales,stock,img_path imgPath from bs_book where id = ?";

        return getBean(Book.class, sql, bookId);
    }

    @Override
    public void updateBook(Book book) {
        String sql = "update bs_book set title=?,author=?,price=?,sales=?,stock=? where id = ?";
        update(sql,book.getTitle(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getId());
    }

   /* @Override
    public List<Book> getBookPageNo(int pageNo, int pageSize) {
        String sql = "select id,title,author,price,sales,stock,img_path imgPath from bs_book limit ?,?";

        return getBeanList(Book.class, sql, (pageNo -1)*pageSize,pageSize);
    }*/

    @Override
    public int getCount() {
        String sql = "select count(*) from bs_book";
        Long count = (Long)getSingleData(sql);
        return count.intValue();
    }

    @Override
    public List<Book> getBookByPage(Page<Book> page) {
        String sql = "select id,title,author,price,sales,stock,img_path imgPath from bs_book limit ?,?";

        return getBeanList(Book.class, sql, (page.getPageNo() -1)*page.getPageSize(),page.getPageSize());
    }


    @Override
    public int getCountByPrice(int minPrice, int maxPrice) {
        String sql = "select count(*) from bs_book  where price between ? and ?";
        Long count = (Long)getSingleData(sql,minPrice,maxPrice);
        return count.intValue();
    }

    @Override
    public List<Book> getBookByPageAndPrice(Page<Book> page, int minPrice, int maxPrice) {
        String sql = "select id,title,author,price,sales,stock,img_path imgPath from bs_book where price between ? and ? limit ?,?";
        return getBeanList(Book.class, sql, minPrice,maxPrice,(page.getPageNo() -1)*page.getPageSize(),page.getPageSize());



    }

    @Override
    public void updateSalesAndStock(Integer bookId, Integer count) {
        String sql = "update bs_book set sales = sales + ?, stock = stock - ? where id = ?";
        update(sql, count, count, bookId);
    }
}
